// DialogUserRegistration.cpp : implementation file
//

#include "stdafx.h"
#include "testSVG.h"
#include "DialogUserRegistration.h"
#include ".\dialoguserregistration.h"

//extern int SendEmail(char* email, char* name, char* org);

// CDialogUserRegistration dialog

IMPLEMENT_DYNAMIC(CDialogUserRegistration, CDialog)
CDialogUserRegistration::CDialogUserRegistration(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogUserRegistration::IDD, pParent)
{
}

CDialogUserRegistration::~CDialogUserRegistration()
{
}

void CDialogUserRegistration::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CDialogUserRegistration, CDialog)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
END_MESSAGE_MAP()


// CDialogUserRegistration message handlers

void CDialogUserRegistration::OnBnClickedOk()
{
#ifdef INCLUDE_EMAIL

	char name[200];
	char email[200];
	char organization[200];
	CString strTest;
	int ret;

	//validate entries
	CEdit* userName = (CEdit*) GetDlgItem(IDC_EDIT_NAME);
	CEdit* userEmail = (CEdit*) GetDlgItem(IDC_EDIT_EMAIL);
	CEdit* userOrganization = (CEdit*) GetDlgItem(IDC_EDIT_ORGANIZATION);

	userName->GetWindowText(name, 200);
	userEmail->GetWindowText(email, 200);
	userOrganization->GetWindowText(organization, 200);

	strTest = name;
	if (strTest == "")
	{
		MessageBox("Please enter your name!", "Name entry is required!", MB_OK | MB_ICONINFORMATION);
		return;
	}

	strTest = email;
	if (strTest == "")
	{
		MessageBox("Please enter your e-mail!", "E-mail entry is required!", MB_OK | MB_ICONINFORMATION);
		return;
	}

	strTest = organization;
	if (strTest == "")
	{
		MessageBox("Please enter your organization!", "Organization entry is required!", MB_OK | MB_ICONINFORMATION);
		return;
	}

	strTest = email;
	ret = strTest.Find("@", 0);
	if (ret == -1)
	{
		MessageBox("Please enter a valid e-mail!", "E-mail entry is required!", MB_OK | MB_ICONINFORMATION);
		return;
	}
	ret = strTest.Find(".", 0);
	if (ret == -1)
	{
		MessageBox("Please enter a valid e-mail!", "E-mail entry is required!", MB_OK | MB_ICONINFORMATION);
		return;
	}

	AfxGetApp()->WriteProfileString(_T("SNAVI"), _T("UserName"), _T(name));
	AfxGetApp()->WriteProfileString(_T("SNAVI"), _T("UserEmail"), _T(email));
	AfxGetApp()->WriteProfileString(_T("SNAVI"), _T("Organization"), _T(organization));

	m_email = email;

	ret = SendEmail(email, name, organization);

	if (ret != 0)
	{
		MessageBox("E-mail delivery failed. Please enter a valid e-mail!", "E-mail entry is required!", MB_OK | MB_ICONINFORMATION);
		return;
	}
#endif
	OnOK();
}
